package com.kuzlak.cms.component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.kuzlak.cms.module.sys.entity.SysLoginLog;
import com.kuzlak.cms.module.sys.service.SysLoginLogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;


@Component
public class TokenAdminUtils {
    @Resource
    private SysLoginLogService logService;



    public boolean isToken(String token, String ipAddr) {
        boolean result = false;
        SysLoginLog login = logService.isLogin(token);
        //System.out.println(login);
        if (login != null) {
            //System.out.println("------:"+ipAddr);
            String ip = login.getIp();
            if (ip != null) {
                result = ip.equals(ipAddr);
            } else {
                result = false;
            }

        }
        return result;

    }

    public boolean isToken(String token) {
        boolean result = false;
        SysLoginLog login = logService.isLogin(token);

        if (login != null) {
            result = true;
        }
        return result;
    }

    public SysLoginLog getToken(String token) {
        SysLoginLog login = logService.isLogin(token);
        if (login != null) {
            if (login.getState() == 0) {
                return login;
            } else {
                return null;
            }
        } else {
            return null;
        }

        // return login;
    }


    public SysLoginLog getToken(long userId) {

        QueryWrapper<SysLoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysLoginLog::getUserId, userId).eq(SysLoginLog::getState, 0);
        SysLoginLog login = logService.getOne(queryWrapper);
        if (login != null) {
            if (login.getState() == 0) {
                return login;
            } else {
                return null;
            }
        } else {
            return null;
        }

        // return login;
    }

    public String getToken(SysLoginLog adminToken) {

        LambdaUpdateWrapper<SysLoginLog> userQueryWrapper = Wrappers.<SysLoginLog>lambdaUpdate()
                .eq(SysLoginLog::getUserId, adminToken.getUserId())
                .eq(SysLoginLog::getState, 0);
        SysLoginLog user = new SysLoginLog();
        user.setState(1);
        long count = logService.count(userQueryWrapper);
        if (count > 0) {
            boolean update = logService.update(user, userQueryWrapper);
            if (update) {
                boolean save = logService.save(adminToken);
                if (save) {
                    return adminToken.getToken();
                } else {
                    return "";
                }
            } else {
                return "";
            }
        } else {
            boolean save = logService.save(adminToken);
            if (save) {
                return adminToken.getToken();

            } else {
                return "";
            }
        }


    }




}
